<?php

// +----------------------------------------------------------------------
// | Shop-Demo for ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2022~2023 Anyon <zoujingli@qq.com>
// +----------------------------------------------------------------------
// | 官方网站: https://thinkadmin.top
// +----------------------------------------------------------------------
// | 免责声明 ( https://thinkadmin.top/disclaimer )
// | 会员免费 ( https://thinkadmin.top/vip-introduce )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\data\controller\msjx;

use app\data\model\DataPolicyItem as DataNewsMark;
use app\data\model\DataQiyeBiaoqian;
use app\data\model\DataQiyeXinxiku;
use app\data\service\PolicyService;
use think\admin\Controller;
use think\admin\helper\QueryHelper;
use think\facade\Db;

/**
 * 添加进展
 * Class Mark
 * @package app\data\controller\news
 */
class MsjxBandTag extends Controller
{
    /**
     * 添加进展
     *
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        DataNewsMark::mQuery()->layTable(function ()
        {
            $this->title = '绑定标签';
            $this->zc_id = $this->get['zc_id'];
            //dump($order_id);die;
        }, function (QueryHelper $query)
        {
            $query->where(['policy_id' => $this->get['zc_id']]);
            // $query->like('name')->equal('status,zc_id')->dateBetween('create_at');

        });
    }

    /**
     * 数据列表处理
     * @param  array  $data
     */
    protected function _page_filter(array &$data)
    {
        $enterprisesTags = DataQiyeBiaoqian::query()->where('deleted', 0)->select()->toArray();
        $tagMap          = array_column($enterprisesTags, 'name', 'id');
        foreach ($data as &$v) {
            $vTag          = $v['tags'] ?? '';
            $v['tags_txt'] = '';
            $tagsName      = [];
            if ($tags = explode(',', $vTag)) {
                foreach ($tags as $tag) {
                    $tagsName[] = $tagMap[$tag] ?? '';
                }
            }
            $v['tags_txt'] = $tagsName ? implode(',', $tagsName) : '';
            $v['zc_id']    = $v['policy_id'];
        }

    }

    /**
     * 添加文章标签
     *
     */
    public function add()
    {
        DataNewsMark::mForm('form');
    }

    /**
     * 编辑文章标签
     *
     */
    public function edit()
    {
        DataNewsMark::mForm('form');
    }

    protected function _form_filter(array &$data)
    {
        $zc_id = input('zc_id');
        if ($this->request->isGet()) {
            $tags           = $data['tags'] ?? '';
            $enterpriseList = DataQiyeXinxiku::query();
            if ($tags = $tags ? explode(',', $tags) : null) {
                $row = '(';
                foreach ($tags as $tag) {
                    // $enterpriseList = $enterpriseList->whereFindInSet('qybq', $tag);
                    $row .= "find_in_set('{$tag}',qybq) or ";
                }
                $row            = rtrim($row, 'or ');
                $row            .= ')';
                $enterpriseList = $enterpriseList->whereRaw($row);
            }
            $enterpriseList    = $enterpriseList->where('status', 1)->select()->toArray();
            $enterpriseIds     = $enterpriseList ? array_column($enterpriseList, 'name', 'id') : [];
            $this->enterprises = implode(',', $enterpriseIds);
            $this->qybq        = $data['tags'] ?? '';
            $this->zc_id       = $zc_id;
        } else {

        }
    }

    /**
     * 表单结果处理
     * @param  bool  $state
     * @return void
     */
    protected function _form_result(bool $state, $data)
    {
        if ($state) {
            PolicyService::instance()->enterpriseBandPolicy($data['select'], $data['policy_id'], $data);
            //写入对应的值
            $this->success('添加成功', "javascript:$('#TagsData').trigger('reload')");
        }
    }

    /**
     * 修改文章标签状态
     *
     */
    public function state()
    {
        DataNewsMark::mSave();
    }

    /**
     * 删除文章标签
     *
     */
    public function remove()
    {
        DataNewsMark::mDelete();
    }
}